package com.ctg.itrdc.sysmgr.permission.core.control;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.sysmgr.permission.core.CtgUser;
import com.ctg.itrdc.sysmgr.permission.core.IPermissionService;
import com.ctg.itrdc.sysmgr.permission.core.dto.FuncMenuDTO;
import com.ctg.itrdc.sysmgr.permission.core.utils.BeanPropertiesMapper;
import com.ctg.itrdc.sysmgr.permission.core.utils.WebUtils;

@Controller
@RequestMapping("/permission")
public class PermissionController {

	@Resource
	private IPermissionService permissionService;

	/**
	 * @api {get} /permission/func_comp 当前用户获取模块权限
	 * @apiGroup permission
	 * @apiParam {Long} menuId
	 * @apiParam {Long} sysUserId
	 * @apiParam {Long} sysPostId
	 * @apiName permission
	 * @apiVersion 0.0.1
	 */
	@RequestMapping("/func_comp")
	@ResponseBody
	public CommonResult permission() {
		CtgUser ctgUser = WebUtils.getCtgUser();
		Long sysUserId = ctgUser.getSysUserId();
		Long sysPostId = ctgUser.getSysPostId();
		List<Map<String, Object>> resultList = permissionService.selectCompList(sysUserId, sysPostId);
		Set<String> set = new HashSet<String>();
		if (resultList != null && resultList.size() > 0) {
			for (Map<String, Object> comp : resultList) {
				set.add((String) comp.get("select_value"));
			}
		}
		return new CommonResult(HttpStatus.OK.toString(), "查询成功", set);
	}

	/**
	 * @api {get} /permission/menu_tree 菜单树
	 * @apiGroup permission
	 * @apiName menu_tree
	 * @apiParam {String} sysCode
	 * @apiParam {Long} sysUserId
	 * @apiParam {Long} sysPostId
	 * @apiVersion 0.0.1
	 */
	@RequestMapping("/menu_tree")
	@ResponseBody
	public CommonResult selectMenuTree() {
		CtgUser ctgUser = WebUtils.getCtgUser();
		Long sysUserId = ctgUser.getSysUserId();
		Long sysPostId = ctgUser.getSysPostId();
		List<Map<String, Object>> menuTree = permissionService.selectMenuTree(sysUserId, sysPostId);
		List<FuncMenuDTO> list = BeanPropertiesMapper.mapList2DTOList(FuncMenuDTO.class, menuTree);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("menus", list);
		map.put("user", ctgUser);
		return new CommonResult(HttpStatus.OK.toString(), "", map);
	}

	@RequestMapping("/permission")
	@ResponseBody
	public CommonResult permissionJs(HttpServletResponse res) throws IOException {
		CtgUser ctgUser = WebUtils.getCtgUser();
		Long sysUserId = ctgUser.getSysUserId();
		Long sysPostId = ctgUser.getSysPostId();
		List<Map<String, Object>> menuTree = permissionService.selectMenuTree(sysUserId, sysPostId);
		List<Map<String, Object>> resultList = permissionService.selectCompList(sysUserId, sysPostId);
		Set<String> set = new HashSet<String>();
		if (resultList != null && resultList.size() > 0) {
			for (Map<String, Object> comp : resultList) {
				set.add((String) comp.get("select_value"));
			}
		}
		Map<String, Object> result = new HashMap<String, Object>(2);
		result.put("menus", menuTree);
		result.put("comps", set);
		return new CommonResult(HttpStatus.OK.toString(), "", result);
	}
}
